//
//  TZGuideViewController.swift
//  SimpleTZ
//
//  Created by wangfeng on 2020/7/9.
//  Copyright © 2020 davis. All rights reserved.
//

import UIKit
import SnapKit

let pageTintColor = UIColor.white.withAlphaComponent(0.5)
let currentTintColor = UIColor.red
let FIRST_IN_KEY = "FIRST_IN_KEY"


class TZGuideCell: UICollectionViewCell {
    let img : UIImageView = UIImageView()
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.addSubview(img)
        img.snp.makeConstraints {
            $0.edges.equalToSuperview()
        }
    }
    required init?(coder aDecoder: NSCoder) {
           fatalError("init(coder:) has not been implemented")
    }
    
    func configData(imgName: String) {
        img.image = UIImage(named: imgName)
    }
    

}



class TZGuideViewController: UIViewController {
    lazy var arrImgAry = [String]()
    var guideBlock: ((_ isFinish: Bool) -> Void)?
    lazy var myCollectionView : UICollectionView = {
              let flowLayout : UICollectionViewFlowLayout = UICollectionViewFlowLayout()
               flowLayout.itemSize = UIScreen.main.bounds.size
               flowLayout.minimumLineSpacing = 0
               flowLayout.minimumInteritemSpacing = 0
               flowLayout.sectionInset = UIEdgeInsets.zero
               flowLayout.scrollDirection = .horizontal
               
               let collectionView: UICollectionView = UICollectionView(frame: self.view.bounds, collectionViewLayout: flowLayout)
               collectionView.showsVerticalScrollIndicator = false
               collectionView.showsHorizontalScrollIndicator = false
               collectionView.backgroundColor = .white
               collectionView.register(TZGuideCell.self, forCellWithReuseIdentifier: "TZGuideCell")
               collectionView.delegate = self
               collectionView.dataSource = self
        collectionView.isPagingEnabled = true
        return collectionView
    }()
    
    lazy var pageControl: UIPageControl = {
        let pageControl = UIPageControl()
        pageControl.backgroundColor = .clear
        pageControl.numberOfPages = 4
        pageControl.alpha = 0
        return pageControl
    }()
    
    lazy var guideBtn: UIButton = {
        let btn = UIButton(type: .custom)
        btn.setTitle("立即体验", for: .normal)
        btn.setTitleColor(.white, for: .normal)
        btn.titleLabel?.font = UIFont.systemFont(ofSize: 18)
        btn.backgroundColor =  .green
        btn.addTarget(self, action: #selector(guideAction), for: .touchUpInside)
        btn.isHidden = true
        return btn
    }()
    
    //MARK: -- 构造方法
    class func GuideVC(guideBlock: @escaping ((_ isFinish:Bool) -> Void)) -> TZGuideViewController{
        let VC = TZGuideViewController()
        VC.guideBlock = guideBlock
        return VC
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        for i in 0..<3 {
            self.arrImgAry.append("welcome\(i)")
        }
        
        setupSubViews()

        // Do any additional setup after loading the view.
    }
    
    //初始化视图
    func setupSubViews()  {
        // 界面样式
        
        view.addSubview(myCollectionView)
        myCollectionView.snp.makeConstraints {
            $0.edges.equalToSuperview()
        }
        view.addSubview(pageControl)
        pageControl.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.bottom.equalToSuperview().offset(-80)
        }
        view.addSubview(guideBtn)
        guideBtn.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.bottom.equalTo(pageControl.snp_top).offset(-50)
        }
    }
    
    
   @objc func guideAction() {
    
    let version = Bundle.main.infoDictionary!["CFBundleShortVersionString"] as! String
    UserDefaults.standard.set(version, forKey: FIRST_IN_KEY)
    UserDefaults.standard.synchronize()
    guideBlock?(true)
    }
    class func hadLoaded() -> Bool{
        let version = Bundle.main.infoDictionary!["CFBundleShortVersionString"] as! String
              let versionCache  = UserDefaults.standard.value(forKey: FIRST_IN_KEY)
              if let a = versionCache , version == (a as! String) {
                 return true
              }
              return false
    }
}

extension TZGuideViewController : UICollectionViewDelegate, UICollectionViewDataSource{
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        self.arrImgAry.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let collectionViewCell : TZGuideCell = collectionView.dequeueReusableCell(withReuseIdentifier: "TZGuideCell", for: indexPath) as! TZGuideCell
        collectionViewCell.configData(imgName: arrImgAry[indexPath.row])
        return collectionViewCell
    }
    
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
       let index =  Int(scrollView.contentOffset.x / self.view.frame.width)
        
        pageControl.currentPage = index
        
        if index == self.arrImgAry.count - 1{
            guideBtn.isHidden = false
        }else{
            guideBtn.isHidden = true
        }
    }
    
}
